#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re

from pyquery import PyQuery

from base.gsxt_base_worker import GsxtBaseWorker
from common import util
from common.global_field import Model

'''
1. 搜索没有结果判断功能添加
'''


class GsxtJiangSuWorker(GsxtBaseWorker):
    def __init__(self, **kwargs):
        GsxtBaseWorker.__init__(self, **kwargs)

    # 需要存入无搜索结果
    def get_search_result_html(self, keyword, session):
        param_list = []
        try:
            url = 'http://{host}/province'.format(host=self.host)
            content = self.get_captcha_geetest(url, 'input#name', '#popup-submit', keyword, 'div.main.search-result',
                                               proxy_type=2)
            if content is None:
                return param_list, self.SEARCH_ERROR

            if content.find('该IP在一天内超过了查询的限定次数') != -1:
                self.log.warn('IP 被封, 无法访问')
                return param_list, self.SEARCH_ERROR

            if content.find('无相关数据') != -1:
                return param_list, self.SEARCH_NOTHING_FIND

            jq = PyQuery(content, parser='html')
            item_list = jq.find('div.main.search-result').find('a').items()
            for item in item_list:
                href = item.attr('href')
                if href is None or href == '':
                    continue
                param_list.append({'href': href})
        except Exception as e:
            self.log.exception(e)
            return param_list, self.SEARCH_ERROR

        return param_list, self.SEARCH_SUCCESS if len(param_list) > 0 else self.SEARCH_ERROR

    def __get_company_name(self, text):
        try:
            if 'CORP_NAME' in str(text):
                name = str(text).split('CORP_NAME":"')[1].split('"')[0]
                return name
        except Exception as e:
            self.log.exception(e)
            return None

        return None

    def get_year_info_list(self, text):
        jq = PyQuery(text, parser='html')
        item_list = jq.find("#yearreportTable").find('tr').items()
        for item in item_list:
            try:
                if item.text().find('年度报告') == -1:
                    continue
                year_info = item.find('td').eq(1)
                if year_info is None or year_info == '':
                    continue

                year_list = re.findall('(\d+)', year_info.text())
                year = str(year_list[0]) if len(year_list) > 0 else None
                if year is None:
                    continue

                href = item.find('a').attr('href')
                if href is None or href == '':
                    continue

                yield year, 'http://{host}{href}'.format(host=self.host, href=href)
            except Exception as e:
                self.log.exception(e)

    # 基本信息
    def get_base_info(self, session, param_dict):
        # 获取基础信息
        org = param_dict['org']
        i_d = param_dict['id']
        seq_id = param_dict['seqId']
        url = 'http://{host}/ecipplatform/publicInfoQueryServlet.json?pageView=true&org={org}&id={id}&seqId={seqId}&abnormal=&activeTabId=&tmp=35'.format(
            org=org, id=i_d, seqId=seq_id, host=self.host)
        base_info = self.task_request(session.get, url)
        if base_info is None:
            self.log.error('获取基础信息页面失败...')
            return None, None

        return url, base_info.text

    # 出资信息
    def get_contributive_info(self, session, param_dict, data):
        org = param_dict['org']
        i_d = param_dict['id']
        seq_id = param_dict['seqId']
        try:
            url = 'http://{host}/ecipplatform/publicInfoQueryServlet.json?queryGdcz=true&abnormal=&activeTabId=&admitMain=08&curPage=1&id={id}&org={org}&pageSize=200&seqId={seqId}&sortName=&sortOrder=&tmp=35'.format(
                id=i_d, seqId=seq_id, org=org, host=self.host)
            r = self.task_request(session.get, url)
            if r is None:
                return

            self.append_model(data, Model.contributive_info, url, r.text)
        except Exception as e:
            self.log.exception(e)

    # 主要人员
    def get_key_person_info(self, session, param_dict, data):
        org = param_dict['org']
        i_d = param_dict['id']
        seq_id = param_dict['seqId']
        reg_no = param_dict['regNo']
        uni_scid = param_dict['uniScid']
        url = 'http://{host}/ecipplatform/publicInfoQueryServlet.json?queryZyry=true&org={org}&id={id}&seqId={seqId}&abnormal=&activeTabId=&tmp=75&regNo={regNo}&admitMain=08&uniScid={uniScid}'.format(
            id=i_d, seqId=seq_id, org=org, host=self.host, regNo=reg_no, uniScid=uni_scid)
        r = self.task_request(session.get, url)
        if r is None:
            return
        self.append_model(data, Model.key_person_info, url, r.text)

    # 变更信息
    def get_change_info(self, session, param_dict, data):
        org = param_dict['org']
        i_d = param_dict['id']
        seq_id = param_dict['seqId']
        reg_no = param_dict['regNo']
        uni_scid = param_dict['uniScid']

        post_data = {
            'org': org,
            'id': i_d,
            'seqId': seq_id,
            'abnormal': '',
            'activeTabId': '',
            'tmp': 75,
            'regNo': reg_no,
            'admitMain': '08',
            'uniScid': uni_scid,
            'pageSize': 100,
            'curPage': 1,
            'sortName': '',
            'sortOrder': '',
        }

        url = 'http://{host}/ecipplatform/publicInfoQueryServlet.json?queryBgxx=true'.format(host=self.host)
        r = self.task_request(session.post, url, data=post_data)
        if r is None:
            return
        self.append_model(data, Model.change_info, url, r.text, post_data=data)

    def get_annual_info(self, session, text, data):
        # 暂时不处理 跟解析沟通一下
        for year, url in self.get_year_info_list(text):
            r = self.task_request(session.get, url)
            if r is None:
                continue
            self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

    def get_shareholder_info(self, session, param_dict, data):
        org = param_dict['org']
        i_d = param_dict['id']
        seq_id = param_dict['seqId']
        try:
            url = 'http://{host}/ecipplatform/publicInfoQueryServlet.json?queryQyjsxxGdcz=true&abnormal=&activeTabId=&admitMain=08&curPage=1&id={id}&org={org}&pageSize=100&seqId={seqId}&sortName=&sortOrder=&tmp=31'.format(
                id=i_d, seqId=seq_id, org=org, host=self.host)
            r = self.task_request(session.get, url)
            if r is None:
                return
            self.append_model(data, Model.shareholder_info, url, r.text)
        except Exception as e:
            self.log.exception(e)

    def get_branch_info(self, session, param_dict, data):
        org = param_dict['org']
        i_d = param_dict['id']
        seq_id = param_dict['seqId']
        reg_no = param_dict['regNo']
        uni_scid = param_dict['uniScid']
        url = 'http://{host}/ecipplatform/publicInfoQueryServlet.json?queryFzjg=true&org={org}&id={id}&seqId={seqId}&abnormal=&activeTabId=&tmp=75&regNo={regNo}&admitMain=08&uniScid={uniScid}'.format(
            id=i_d, seqId=seq_id, org=org, host=self.host, regNo=reg_no, uniScid=uni_scid)
        r = self.task_request(session.get, url)
        if r is None:
            return
        self.append_model(data, Model.branch_info, url, r.text)

    def parse_param(self, href):
        # /ecipplatform/jiangsu.jsp?org=8AD843CF9D91B3B2BB82982272E267AB&id=CF61AECE82AEA4651C259CEDA4126DA0&seqId=8A0E429E8B038E7017660C378B89542C&activeTabId=
        param_dict = {}
        try:
            sub_url_list = href.split('?')
            if len(sub_url_list) != 2:
                return None

            sub_url = sub_url_list[1]
            sub_list = sub_url.split('&')
            if len(sub_list) < 3:
                return None

            for item in sub_list:
                item_list = item.split('=')
                if len(item_list) < 2:
                    continue

                param_dict[item_list[0]] = item_list[1]

            if 'org' not in param_dict or 'id' not in param_dict or 'seqId' not in param_dict:
                return None

        except Exception as e:
            self.log.exception(e)
            return None

        return param_dict

    @staticmethod
    def get_keyword_info(base_text):
        # "REG_NO":"913202052500830484", "UNI_SCID":"97F2A486B9901B971CCF8E9FA95B14A3"
        json_data = util.json_loads(base_text)
        if json_data is None:
            return None, None

        reg_no = json_data.get('REG_NO', None)
        uni_scid = json_data.get('UNI_SCID', None)

        return reg_no, uni_scid

    def get_detail_html_list(self, seed, session, param_list):
        # 保存企业名称
        data_list = []
        rank = 0
        error_num_count = 0
        for item in param_list:
            try:
                href = item.get('href', None)
                if href is None:
                    self.log.error('参数存储异常: item = {item}'.format(item=item))
                    continue

                param_dict = self.parse_param(href)
                if param_dict is None:
                    self.log.error('参数信息错误: href = {href}'.format(href=href))
                    error_num_count += 1
                    continue

                # 基本信息
                url, base_text = self.get_base_info(session, param_dict)
                if url is None or base_text is None:
                    continue

                # 获得公司名称
                company_name = self.__get_company_name(base_text)
                if company_name is None or company_name == '':
                    self.log.error('公司名称解析失败..param_dict = {param_dict} {text}'.format(
                        text=base_text, param_dict=param_dict))
                    continue

                # 建立数据模型
                rank += 1
                data = self.get_model(company_name, rank, seed, self.province)

                # 存储数据
                self.append_model(data, Model.base_info, url, base_text)

                # 出资信息
                self.get_contributive_info(session, param_dict, data)

                reg_no, uni_scid = self.get_keyword_info(base_text)
                if reg_no is not None and uni_scid is not None:
                    param_dict['regNo'] = reg_no
                    param_dict['uniScid'] = uni_scid

                    # 主要人员信息
                    self.get_key_person_info(session, param_dict, data)

                    # 分支机构
                    self.get_branch_info(session, param_dict, data)

                    # 变更信息
                    self.get_change_info(session, param_dict, data)

                # 获得年报信息
                # 6.5W家年报暂时满足要求 可暂缓
                # self.get_annual_info(session, base_text, data)

                # 股东信息
                self.get_shareholder_info(session, param_dict, data)

                data_list.append(data)
            except Exception as e:
                self.log.exception(e)
        self.target_db.insert_batch_data(self.target_table, data_list)
        return len(data_list) + error_num_count
